﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class InternalSDKManagerScript {

	/* Interface to native implementation */
	#if UNITY_IOS
	[DllImport ("__Internal")]
	private static extern void _setGameObjectNameForInternal(string gameObjectName);

	[DllImport ("__Internal")]
	private static extern void _popAlertDialog(string message);

	[DllImport ("__Internal")]
	private static extern void _startToCheckNetwork();

	[DllImport ("__Internal")]
	private static extern void _showNetworkAlert();

	[DllImport ("__Internal")]
	private static extern float _densityScale ();

	[DllImport ("__Internal")]
	private static extern bool _checkNetworkAvailable();

	[DllImport ("__Internal")]
	private static extern bool _isTablet();

	[DllImport ("__Internal")]
	private static extern void _showMoreGames();

	[DllImport ("__Internal")]
	private static extern void _doNewsBlast();

	[DllImport ("__Internal")]
	private static extern void _saveToAlbum(byte[] imageBytes, int dataLength);

	[DllImport ("__Internal")]
	private static extern void _rateUs();

	[DllImport ("__Internal")]
	private static extern void _sendEmail(string subject, string body);

	[DllImport ("__Internal")]
	private static extern void _sendEmailAndFilePic(string subject, string message, string fileName);


	[DllImport ("__Internal")]
	private static extern void _showLoadingView();

	[DllImport ("__Internal")]
	private static extern void _removeLoadingView();

	#endif

	public void setGameObjectNameForInternal(string gameObjectName) 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().setGameObjName(gameObjectName);
			#elif UNITY_IOS
			_setGameObjectNameForInternal(gameObjectName);
			#endif
		}
	}

	public void popAlertDialog(string message)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().popAlertDialog(message);
			#elif UNITY_IOS
			_popAlertDialog (message);
			#endif
		}
	}
		
	public void startToCheckNetwork()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			//SSCInternalAdapter.getInstance().startToCheckNetwork();
			#elif UNITY_IOS
			_startToCheckNetwork ();
			#endif
		}
	}

	public void showNetworkAlert()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().showNetworkAlert();
			#elif UNITY_IOS
			_showNetworkAlert ();
			#endif
		}
	}

	public bool checkNetworkAvailable() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCInternalAdapter.getInstance().checkNetworkAvailable();
			#elif UNITY_IOS
			return _checkNetworkAvailable ();
			#endif
		}
			
		return false;
	}

	public bool isTablet() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCInternalAdapter.getInstance().isTablet();
			#elif UNITY_IOS
			return _isTablet ();
			#endif
		}

		return false;
	}

	public void showMoreGames() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().showMoreGames();
			#elif UNITY_IOS
			_showMoreGames ();
			#endif
		}
	}

	public void doNewsBlast() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().doNewsBlast();
			#elif UNITY_IOS
			_doNewsBlast ();
			#endif
		}
	}

	public void refreshDCIM(string sFileDir) 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().refreshDCIM(sFileDir);
			#elif UNITY_IOS
			#endif
		}
	}

	public string getSDCardPath() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCInternalAdapter.getInstance().getSdcardPath();
			#elif UNITY_IOS
			#endif
		}

		return "";
	}

	public void makeToast(string message) 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().makeToast(message);
			#elif UNITY_IOS
			#endif
		}
	}

	public int listAssetFiles(string path) 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCInternalAdapter.getInstance().listAssetFiles(path);
			#elif UNITY_IOS
			#endif
		}

		return 0;
	}

	public void saveToAlbum(byte[] imageBytes, int dataLength,string dir)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().saveImageToAlbum(dir,imageBytes);
			#elif UNITY_IOS
			_saveToAlbum (imageBytes,dataLength);
			#endif
		}
	}

	public float densityScale()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCInternalAdapter.getInstance().densityScale();
			#elif UNITY_IOS
			return _densityScale();
			#endif
		}

		return 0.0f;
	}

	public void rateUs()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().rateUs();
			#elif UNITY_IOS
			_rateUs ();
			#endif
		}
	}

	public void sendEmail(string subject, string body)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().sendEmail(subject,body);
			#elif UNITY_IOS
			_sendEmail (subject,body);
			#endif
		}
	}

	public void sendEmailAndFilePic(string subject, string message, string fileName)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().sendEmailAndFilePic(subject,message,fileName);
			#elif UNITY_IOS
			_sendEmailAndFilePic (subject,message,fileName);
			#endif
		}
	}

	public void endSession()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().endSession();
			#elif UNITY_IOS
			#endif
		}
	}

	public void showLoadingView()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().showLoadingView();
			#elif UNITY_IOS
			_showLoadingView();
			#endif
		}
	}

	public void removeLoadingView()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCInternalAdapter.getInstance().removeLoadingView();
			#elif UNITY_IOS
			_removeLoadingView();
			#endif
		}
	}

}
